package com.example.springcloud.demo;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import redis.clients.jedis.Jedis;

import java.lang.management.*;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.LongAdder;

public class RunDemo {

//    public static void main(String[] args) throws Exception {
////        redisTest();
//    }

    public static Jedis jedis = new Jedis("127.0.0.1", 6379);

    public static void redisTest() throws Exception {
        Long m1 = Long.valueOf(getMemory());
        insertData();
        Long m2 = Long.valueOf(getMemory());
        System.out.println(m2 - m1);
    }

    public static void insertData() {
        for (int i = 10000; i < 10100; i++) {
            jedis.set("aa" + i, "aa" + i); //key和value长度都是7字节，且不是整数
        }
    }

    public static String getMemory() {
        String memoryAllLine = jedis.info("memory");
        String usedMemoryLine = memoryAllLine.split("\r\n")[1];
        String memory = usedMemoryLine.substring(usedMemoryLine.indexOf(':') + 1);
        return memory;
    }


    static LongAdder longAdder = new LongAdder();
    private static final ThreadFactory NAMED_THREAD_FACTORY = new ThreadFactoryBuilder()
            .setNameFormat("biz-thread-pool-%d").build();
    public static final ExecutorService FIXED_THREAD_POOL = new ThreadPoolExecutor(5, 200, 0L, TimeUnit.MICROSECONDS, new LinkedBlockingDeque<>(1024), NAMED_THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy());

    public static void run() {
//        new ThreadTest().start();
        FIXED_THREAD_POOL.execute(new ThreadNewObjectTest());
        FIXED_THREAD_POOL.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
//                        System.err.println("count=" + longAdder.longValue());
                        TimeUnit.SECONDS.sleep(1);
                        longAdder.reset();
                    } catch (Exception ex) {

                    }
                }
            }
        });

    }

    public static class ThreadNewObjectTest implements Runnable {
        @Override
        public void run() {
            while (true) {
                byte[] b = new byte[16 + (int) (Math.random() * 100)];
                longAdder.increment();
            }
        }
    }

    public static class ThreadTest extends Thread {
        @Override
        public void run() {
            super.run();
            while (true) {
                try {
                    System.out.println("=====================================");
                    //Java虚拟机的内存系统
                    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
                    System.out.println("虚拟机的堆内存使用量: " + memoryMXBean.getHeapMemoryUsage());
                    System.out.println("虚拟机的非堆内存使用量: " + memoryMXBean.getNonHeapMemoryUsage());
                    //Java虚拟机的类加载系统
                    ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
                    System.out.println("当前加载到Java虚拟机中的类的数量: " + classLoadingMXBean.getLoadedClassCount());
                    System.out.println("自Java虚拟机开始执行到目前已经加载的类的总数: " + classLoadingMXBean.getTotalLoadedClassCount());
                    //Java虚拟机的线程系统
                    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
                    System.out.println("当前线程的总CPU时间: " + threadMXBean.getCurrentThreadCpuTime());
                    System.out.println("当前活动线程的数目，包括守护线程和非守护线程: " + threadMXBean.getThreadCount());
                    //Java虚拟机的线程系统
                    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
                    System.out.println("当前Java库路径: " + runtimeMXBean.getLibraryPath());
                    System.out.println("当前Java虚拟机实现提供商: " + runtimeMXBean.getVmVendor());
                    //操作系统
                    OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
                    System.out.println("当前Java虚拟机可以使用的处理器数目: " + operatingSystemMXBean.getAvailableProcessors());
                    System.out.println("当前操作系统名称: " + operatingSystemMXBean.getName());
                    //Java虚拟机的编译系统
                    CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
                    System.out.println("当前(JIT)编译器的名称: " + compilationMXBean.getName());
                    System.out.println("当前即时(JIT)编译器的名称: " + compilationMXBean.getTotalCompilationTime());
                    //Java虚拟机的垃圾回收系统
                    List<GarbageCollectorMXBean> garbageCollectorMXBeanList = ManagementFactory.getGarbageCollectorMXBeans();
                    for (GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeanList) {
                        System.out.println("当前垃圾收集器的名字: " + garbageCollectorMXBean.getName());
                        System.out.println("当前垃圾收集器累计回收总次数: " + garbageCollectorMXBean.getCollectionCount());
                        System.out.println("当前垃圾收集器累计回收总时间: " + garbageCollectorMXBean.getCollectionTime());
                    }
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception ex) {

                }
            }
        }
    }
}
